import http from "../../axios/index.js";
import { handleSuccess, handleError } from "../index.js";

export const UserApi = {
    //获取用户列表
    getUserList: (params) => {
        return http.get('/api/user/list',{ params });
    },
    //注册
    register: (data) => {
        return http.post('/register', { data:data})
            .then(res => handleSuccess(res, '注册成功'))
            .catch(error =>handleError(error, '注册失败', '服务器连接异常'));
    },
    //获取用户详情
    getUserDetail: (id) => {
        return http.get(`/api/user/detail/${id}`)
        .then(res => handleSuccess(res, '获取用户信息成功'))
        .catch(error =>handleError(error, '获取用户信息失败', '服务器连接异常'));
    },
    //获取用户权限信息
    getUserAuthInfo: () => {
        return http.get('/api/auth/info')
            .then(res => handleSuccess(res, '获取用户权限信息成功'))
            .catch(error =>handleError(error, '获取用户权限信息失败', '服务器连接异常'));
    },
    //登录
    login: (data) => {
        return http.post('/api/auth/login', data)
        .then(res => handleSuccess(res, '登录成功'))
        .catch(error =>handleError(error, '登录失败', '服务器连接异常'));
    },
    //验证码登录
    smsLogin: (params) => {
        return http.post('/api/auth/smsLogin', {},{params:{...params}})
        .then(res => handleSuccess(res, '手机号登录成功'))
        .catch(error =>handleError(error, '手机号登录失败', '服务器连接异常'));
    },
    //退出登录
    logout: () => {
        return http.post('/logout')
       .then(res => handleSuccess(res, '退出登录成功'))
       .catch(error =>handleError(error, '退出登录失败', '服务器连接异常'));
    },
    //更新用户信息
    updateUserInfo: (data) => {
        return http.put('/api/user/update', data)
        .then(res => handleSuccess(res, '更新用户信息成功'))
        .catch(error =>handleError(error, '更新用户信息失败', '服务器连接异常'));
    },
    //删除用户
    deleteUser: (id) => {
        return http.delete(`/api/user/delete/${id}`)
            .then(res => handleSuccess(res, '删除用户成功'))
            .catch(error => handleError(error, '删除用户失败', '服务器连接异常'));
    },
    //获取验证码
    getSsmCode: (phone) => {
        return http.put(`/api/user/phone/code`, {},{params:{phone}})
        .then(res => handleSuccess(res, '验证码发送成功'))
        .catch(error =>handleError(error, '验证码发送失败', '服务器连接异常'));
    },
    //更换绑定手机号
    updatePhone: (params) => {
        return http.put('/api/user/phone', {},{params:{...params}})
        .then(res => handleSuccess(res, '更换绑定手机号成功'))
        .catch(error =>handleError(error, '更换绑定手机号失败', '服务器连接异常'));
    },
    //实名认证
    authSubmit: (data) => {
        return http.post('/api/user/auth-submit', data)
        .then(res => handleSuccess(res, '实名认证成功'))
        .catch(error =>handleError(error, '实名认证失败', '服务器连接异常'));
    },
    //获取待审核实名认证列表
    getAuthPendingList: () => {
        return http.get('/api/admin/user/auth-pending-list')
        .then(res => handleSuccess(res, '获取待审核实名认证列表成功'))
        .catch(error =>handleError(error, '获取待审核实名认证列表失败', '服务器连接异常'));
    },
    //获取实名认证状态
    getAuthStatus: () => {
        return http.get(`/api/user/auth-status`)
        .then(res => handleSuccess(res, '获取实名认证状态成功'))
        .catch(error =>handleError(error, '获取实名认证状态失败', '服务器连接异常'));
    },
    //获取个人信息
    getUserProfile: () => {
        return http.get(`/api/user/profile`)
        .then(res => handleSuccess(res, '获取个人信息成功'))
        .catch(error =>handleError(error, '获取个人信息失败', '服务器连接异常'));
    },
    //更新个人信息
    updateUserProfile: (params) => {
        return http.put(`/api/user/updateProfile`, {},{params:{...params}})
        .then(res => handleSuccess(res, '更新个人信息成功'))
        .catch(error =>handleError(error, '更新个人信息失败', '服务器连接异常'));
    },
    //修改密码
    updatePassword: (params) => {
        return http.put(`/api/user/password`, {},{params:{...params}})
        .then(res => handleSuccess(res, '修改密码成功'))
        .catch(error =>handleError(error, '修改密码失败', '服务器连接异常'));
    },
    //实名认证审核
    authVerify: (params) => {
        return http.put(`/api/admin/user/auth-verify`, {},{params:{...params}})
        .then(res => handleSuccess(res, '实名认证审核成功'))
        .catch(error =>handleError(error, '实名认证审核失败', '服务器连接异常'));
    }

}